<template>
  <div class="container">
    <navigate>修改登录密码</navigate>
    <div class="outer">
      <van-cell-group class="input_outer">
        <van-field
          class="nowrap"
          type="password"
          v-model="oldPwd"
          label="*验证旧密码："
          placeholder="请输入旧密码"
        />
      </van-cell-group>
      <van-cell-group class="input_outer">
        <van-field
          class="nowrap"
          type="password"
          v-model="newPwd"
          label="*新的密码"
          placeholder="请输入新密码"
        />
      </van-cell-group>
      <van-cell-group class="input_outer">
        <van-field
          class="nowrap"
          type="password"
          v-model="againPwd"
          label="*重复输入"
          placeholder="请再次输入密码"
        />
      </van-cell-group>
      <button class="btn" @click="updatePassword">提交修改</button>
    </div>
  </div>
</template>

<script>
import { alterPassword } from "@/api/my/index";
export default {
  data() {
    return {
      oldPwd: "",
      newPwd: "",
      againPwd: "",
    };
  },
  methods: {
    async updatePassword() {
      if (!this.oldPwd) {
        return this.$toast.fail("请输入旧密码！");
      }
      if (!this.newPwd) {
        return this.$toast.fail("请输入新密码！");
      }
      if (!this.againPwd) {
        return this.$toast.fail("请再次输入新密码！");
      }
      if (this.newPwd != this.againPwd)
        return this.$toast.fail("两次密码不一致!");
      let par = {
        password: this.oldPwd,
        new_password: this.newPwd,
        new_password2: this.againPwd,
      };

      let res = await alterPassword(par);
      if (res.code == 200) {
        this.$toast.success("修改成功！");
        this.$router.push("/");
      }
    },
  },
};
</script>

<style lang="less" scoped>
@import "./css/index.less";
</style>
